package models;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import model.entityes.exception.AllTypesException;

import play.data.validation.Constraints.Required;
import play.db.ebean.Model;

@Entity
@Table(name = "all_types")
public class AllTypes extends Model
{
    private static final long serialVersionUID = 1L;

    @Id
    protected String short_name;
    @Required
    protected String table_name;
    protected String type_name;

    protected static String SHORT_NAME = "short_name";
    protected static String TABLE_NAME = "table_name";
    protected static String TYPE_NAME = "type_name";

    public AllTypes getTypeById(String shortName) throws AllTypesException
    {
	try
	{
	    List<AllTypes> all = AllTypes.find.where()
		    .ilike(AllTypes.SHORT_NAME, shortName).findList();
	    if (all.size() != 1) throw AllTypesException
		    .errorGetById(shortName);
	    return all.get(0);
	}
	catch (Exception e)
	{
	    throw AllTypesException.getExceptionWhitUserMessage("Ошибка: "
		    + e.getMessage());
	}
    }

    public List<AllTypes> searchByName(String name) throws AllTypesException
    {
	try
	{
	    return AllTypes.find.where().ilike(AllTypes.TYPE_NAME, name)
		    .findList();
	}
	catch (Exception e)
	{
	    throw AllTypesException.getExceptionWhitUserMessage("Ошибка: "
		    + e.getMessage()); 
	}
    }

    public static Model.Finder<Long, AllTypes> find = new Model.Finder<Long, AllTypes>(
	    Long.class, AllTypes.class);

    public String getShort_name()
    {
	return short_name;
    }

    public void setShort_name(String short_name)
    {
	this.short_name = short_name;
    }

    public String getTable_name()
    {
	return table_name;
    }

    public void setTable_name(String table_name)
    {
	this.table_name = table_name;
    }

    public String getType_name()
    {
	return type_name;
    }

    public void setType_name(String type_name)
    {
	this.type_name = type_name;
    }

}
